<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\model\Admin;

class CustomController extends Controller
{
    /**
     * 获取所有的客服信息
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // dd($request->all());
        $search = $request->input('search',''); //搜索条件，没有则默认为空
        $count = $request ->input('count',10); //每页显示几条数据，默认为10
        $request = $request->all();
        $custom = Admin::where('role_id',3)->where('a_uname','like','%'.$search.'%')->paginate($count);
        return view('admin.ll.custom.index',['custom'=>$custom,'request'=>$request]);
    }
    /**
    *修改客服用户状态(禁用或者开启)
    *@param $id =>'用户id'
    */
    public function editById($id){
        $custom = Admin::find($id);
        if($custom->a_status==0){
            $custom->a_status=1;
        }else{
            $custom->a_status=0;
        }
        $custom->save();
        return back();
    }

    /**
     * 显示添加客服的界面
     *
     * @return 
     */
    public function create()
    {
        return view('admin.ll.custom.create');
    }

    /**
     * 执行添加客服操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except(['_token','a_rpass']);
        $custom =new Admin;
        $custom->a_uname = $data['a_uname'];
        $custom->a_upass = $data['a_upass'];
        $custom->a_name = $data['a_name'];
        $custom->a_phone = $data['a_phone'];
        $custom->role_id = 3;
        if($custom->save()){
            echo '<script>alert("添加成功");window.location="/t_admin/custom"</script>';
        }else{
            echo '<script>alert("添加失败");window.location="/t_admin/custom/create"</script>';
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 根据id修改客服数据
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //获取数据
        $custom = Admin::find($id);
        return view('admin.ll.custom.edit',['custom'=>$custom]);
    }

    /**
     * 修改客服数据
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $custom = Admin::find($id);
        $data = $request->except('_token');
        $custom->a_name = $data['a_name'];
        $custom->a_phone = $data['a_phone'];
        if($custom->save()){
            echo '<script>alert("修改成功");window.location="/t_admin/custom"</script>';
        }else{
            echo '<script>alert("修改失败");window.location="/t_admin/custom/'.$id.'/edit"</script>';
        }
    }

    /**
     * 根据id删除客服数据
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $custom = Admin::find($id);
        $data =[];

        if($custom->delete()){
            $data['status'] = 0;
            $data['msg'] = '删除成功';
        }else{
            $data['status']= 1;
            $data['msg']='删除失败';
        }
    }
}
